#!/usr/bin/env python3
# -*- coding: utf-8 -*-

#  Copyright (C) 2020 ShenZhen Powersmart Information Technology Co.,Ltd
#  All Rights Reserved.
#  本软件为深圳博沃智慧开发研制。未经本公司正式书面同意，其他任何个人、团体不得使用、
#  复制、修改或发布本软件.

# @Time : 2025/9/11 17:36
# @File : service_config.py
# @Project: 
# @Author : wanghongtao
# @Software: PyCharm

from typing import Dict, Any, Optional
from dataclasses import dataclass


@dataclass
class ServiceObjectConfig:
    """服务对象配置"""
    service_id: str
    name: str
    description: str
    enabled: bool = True
    model_name: str = "glm-4-flash"
    provider: str = "zhipu"
    max_tokens: int = 2000
    temperature: float = 0.7
    timeout: int = 30
    config: Dict[str, Any] = None
    
    def __post_init__(self):
        if self.config is None:
            self.config = {}


class ServiceParamsConfig:
    """服务功能配置"""
    
    # 信息抽取服务配置
    INFO_EXTRACTION = ServiceObjectConfig(
        service_id="extract_info",
        name="地址信息抽取",
        description="从信访内容中抽取街道、区县、详细地址、详细对象等信息",
        enabled=True,
        model_name="glm-4-flash",
        provider="zhipu",
        max_tokens=2000,
        temperature=0.7,
        timeout=30,
        config={
            "required_fields": ["xfnr"],
            "optional_fields": ["xfgk", "xfwtsd", "xfqxs", "xfxzjd", "xfdz", "xfqxdw", "xfbz", "xfdfnr"],
            "output_format": "json",
            "enable_thinking": False
        }
    )
    
    # 规范分析服务配置
    SPECIFY_ANALYSIS = ServiceObjectConfig(
        service_id="specify_analysis",
        name="规范分析",
        description="根据定义判断回复内容是否合规",
        enabled=True,
        model_name="glm-4-flash",
        provider="zhipu",
        max_tokens=1000,
        temperature=0.3,
        timeout=30,
        config={
            "required_fields": ["dfnr"],
            "output_format": "json",
            "enable_thinking": False,
            "judgment_rules": [
                "被举报对象基本情况",
                "现场检查情况",
                "调查处理情况",
                "对举报投诉是否属实表述",
                "答复举报人情况"
            ]
        }
    )
    
    # 情感分析服务配置
    SENTIMENT_ANALYSIS = ServiceObjectConfig(
        service_id="sentiment_analysis",
        name="情感分析",
        description="根据定义判断信访内容的情感",
        enabled=True,
        model_name="glm-4-flash",
        provider="zhipu",
        max_tokens=1500,
        temperature=0.5,
        timeout=30,
        config={
            "required_fields": ["xfnr"],
            "output_format": "json",
            "enable_thinking": False,
            "sentiment_levels": ["轻度", "中度", "重度"],
            "analysis_features": ["情感分析", "主题词摘要提取"]
        }
    )
    
    # 兼容旧版本的配置
    INFO_EXTRACTION_ENABLED = INFO_EXTRACTION.enabled
    INFO_EXTRACTION_MODEL = INFO_EXTRACTION.model_name
    
    @classmethod
    def get_service_config(cls, service_id: str) -> Optional[ServiceObjectConfig]:
        """根据服务ID获取服务配置"""
        service_map = {
            "extract_info": cls.INFO_EXTRACTION,
            "specify_analysis": cls.SPECIFY_ANALYSIS,
            "sentiment_analysis": cls.SENTIMENT_ANALYSIS
        }
        return service_map.get(service_id)
    
    @classmethod
    def get_all_services(cls) -> Dict[str, ServiceObjectConfig]:
        """获取所有服务配置"""
        return {
            "extract_info": cls.INFO_EXTRACTION,
            "specify_analysis": cls.SPECIFY_ANALYSIS,
            "sentiment_analysis": cls.SENTIMENT_ANALYSIS
        }
    
    @classmethod
    def get_enabled_services(cls) -> Dict[str, ServiceObjectConfig]:
        """获取启用的服务配置"""
        return {
            service_id: config 
            for service_id, config in cls.get_all_services().items() 
            if config.enabled
        }
    
    @classmethod
    def is_service_enabled(cls, service_id: str) -> bool:
        """检查服务是否启用"""
        config = cls.get_service_config(service_id)
        return config is not None and config.enabled
    
    @classmethod
    def update_service_config(cls, service_id: str, **kwargs) -> bool:
        """更新服务配置"""
        config = cls.get_service_config(service_id)
        if not config:
            return False
        
        for key, value in kwargs.items():
            if hasattr(config, key):
                setattr(config, key, value)
        
        return True